package cn.miw.android.bdmp3.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.miw.android.bdmp3.R;
import cn.miw.android.bdmp3.model.BaiDuListItem;
import cn.miw.android.bdmp3.service.Pub;
import cn.miw.android.listViewAdapter.IListViewInitor;

public class InfoInitor implements IListViewInitor {
	private Context context;

	public InfoInitor(Context context) {
		super();
		this.context = context;
	}

	public View initView() {
		ViewHolder holder = new ViewHolder();
		View convertView = LayoutInflater.from(context).inflate(R.layout.music_item, null);
		holder.RankPic = (ImageView) convertView.findViewById(R.id.playState);
		holder.Singer = (TextView) convertView.findViewById(R.id.item_Singer);
		holder.Song = (TextView) convertView.findViewById(R.id.item_Song);
		holder.AlbumnName = (TextView) convertView.findViewById(R.id.item_AlbumnName);
		holder.Dur = (TextView) convertView.findViewById(R.id.item_Dur);
		holder.progressBar = (ProgressBar) convertView.findViewById(R.id.down_progress);
		holder.BtnMenu = (ImageView) convertView.findViewById(R.id.btnMenu);
		holder.BtnMenu.setOnClickListener((OnClickListener) context);
		holder.BtnMenu.setOnCreateContextMenuListener((OnCreateContextMenuListener) context);
		convertView.setTag(holder);
		return convertView;
	}

	public View fillView(View view, Object data, String MOTHED, int position) {
		ViewHolder holder = (ViewHolder) view.getTag();
		BaiDuListItem item = (BaiDuListItem) data;
		holder.BtnMenu.setTag(position);
		holder.Singer.setText(Html.fromHtml(item.getSinger() + ""));
		holder.AlbumnName.setText(Html.fromHtml(item.getAlbumnName() + ""));
		holder.Song.setText(Html.fromHtml(item.getSong() + ""));
		if (MOTHED == "MOTHED_DOWNLIST") {
			holder.progressBar.setVisibility(View.VISIBLE);
			String name = item.getSinger() + "-" + item.getSong();
			BaiDuListItem x = Pub.downListMap.get(name);
			holder.progressBar.setMax(x.getFileLength());
			holder.progressBar.setProgress(x.getCurPos());
			holder.Dur.setText(x.getCurPos() * 100 / x.getFileLength() + "%");
		} else if (MOTHED == "LOCAL_MUSIC") {
			int dur = item.getFileLength();
			String x = dur / 60 + ":" + ("00" + dur % 60).substring(("00" + dur % 60).length() - 2);
			holder.Dur.setText(x);
		} else if (MOTHED == "FOLDER") {
			holder.RankPic.setImageResource(R.drawable.folder);
			holder.Dur.setVisibility(View.GONE);
			holder.BtnMenu.setVisibility(View.GONE);
		}
		return null;
	}

	class ViewHolder {
		public ImageView RankPic, BtnMenu;
		public TextView Singer;
		public TextView Song;
		public TextView AlbumnName;
		public TextView Dur;
		public ProgressBar progressBar;
	}
}
